package org.nf.shopping.service.Impl;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.nf.shopping.common.exception.ErrorsEnum;
import org.nf.shopping.common.exception.LoginException;
import org.nf.shopping.common.exception.PwdException;
import org.nf.shopping.entity.Role;
import org.nf.shopping.mapper.RoleMapper;
import org.nf.shopping.service.RoleService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author lxy
 * @Date 2024-10-15
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {
    private final RoleMapper roleMapper;
    @Override
    public Role auth(Role role) {
        Role role1 = roleMapper.getRole(role);
        if (role != null){
            return role1;
        }
        throw new LoginException(ErrorsEnum.LOGIN_EXCEPTION);
    }

    @Override
    public PageInfo<Role> getRoleListByPage(Integer limit, Integer page, String roleName, String roleType) {
        List<Role> list = roleMapper.listRole(limit, page, roleName,roleType);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void checkPwd(String pwd, String userName, String newPwd) {
        String password = roleMapper.selectRolePwd(pwd, userName);
        if (! "".equals(password)){
            roleMapper.updateRolePwd(newPwd,userName);
        }else {
            throw new PwdException(ErrorsEnum.PWD_EXCEPTION);
        }
    }

    @Override
    public PageInfo<Role> listRole(Integer pageSize, Integer pageNum, String name) {
        List<Role> list = roleMapper.listRoles(pageSize,pageNum,name);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateRole(role);
    }

    @Override
    public void deloneRole(Integer id) {
        roleMapper.deloneRole(id);
    }

    @Override
    public void addRole(Role role) {
        roleMapper.addRole(role);
    }

    @Override
    public List<Role> getRoleList() {
        return roleMapper.getRoleList();
    }
}
